<?php
class Auth extends MY_Controller{

	function login (){
		if($this->checkUserStatus()){
			redirect('dashboard');
		}else{
			if($_POST){
				if($_POST['remember'] = 1)
				{
					if($this->aauth->login($_POST['email'],$_POST['password'],TRUE))
					redirect ('dashboard');
					else
					$data['login_error'] = $this->aauth->get_errors_array();
				}else{
					if($this->aauth->login($_POST['email'],$_POST['password']))
					redirect ('dashboard');
					else
					$data['login_error'] = $this->aauth->get_errors();
				}
					
			}

			$data['title'] = $this->title('Identifiant');
			$this->load->view($this->theme.'/auth/login',$data);


		}
	}

	public function logout() {

		$this->aauth->logout();
		redirect('login');
	}

	function profile()
	{
		$id = $this->uri->segment(2);
			
		if($this->checkUserStatus() ){
			if($id == null){
				redirect('dashboard');
			}elseif($this->aauth->get_user_id() != $id){
				redirect('error_404');
			}else{
					$this->load->library('form_validation');
					$this->form_validation->set_rules('password','mot de passe','required|min_length[8]|max_length[13]|matches[passconf]');
					$this->form_validation->set_rules('passconf','confirmation', 'required');
					$this->form_validation->set_error_delimiters('<p class="text-red">', '</p>');
					$this->form_validation->set_message('rule', 'Error Message');
				if($_POST){
					
					
					if($this->form_validation->run() == false){
						
					}else{
						$newData = $this->aauth->update_user($this->aauth->get_user_id(),false,$_POST['password'],false);
						if($newData){
							$data['msg_success'] = 'Le nouveau mot de passe a été crée. ';
						}else{
							$data['msg_fail'] = 'Il y a une erreur. ';
						}
					}
				}
				$data['toDoList'] = $this->get_tasks();
				$data['title'] = $this->title('profile');
				$data['user'] = $this->aauth->get_user();
				$this->load->view($this->theme.'/header',$data);
				$this->load->view($this->theme.'/auth/profile');
				$this->load->view($this->theme.'/footer');
			}

		}else{
			redirect('login');
		}
			
	}


	function modification ()
	{
		if(!$this->checkUserStatus() ){
			redirect('login');
		}else{
			if(!$_POST){
				redirect('error_404');
			}else{
				$pk = $_POST['pk'];
				$name = $_POST['name'];
				$value = $_POST['value'];
				if(!empty($value) ) {
					if($name == 'email'){
						if($this->checkValidEamil($value)){
							$this->aauth->update_user($pk,$value,false,false);							
							//header('HTTP 200 OK', true, 200);
							echo $name;
						}else{
							//header('HTTP 400 Bad Request', true, 400);
							echo "Le format d'email n'est pas correct!";
						}
					}else{
					$this->aauth->update_user($pk,false,false,$value);	
					//header('HTTP 200 OK', true, 200);
					echo $name;
					}
					
				} else {

					//header('HTTP 400 Bad Request', true, 400);
					echo "Ce champ est obligatoire!";
				}


			}
		}
	}
	

	
	
	function addThumb()
	{
		if($_POST)
		{
			$md5Date = Date("Y_m_d_H_i_s");
			$config['upload_path'] = 'uploads/avatar/thumbs';
			$config['allowed_types'] = 'jpg|png|jpeg';
			$config['max_size'] = '3000';
			$config['file_name'] = 'avatar_'.$md5Date;
			$this->load->library('upload', $config);

			if(!$this->upload->do_upload()){
				$error = array('validate'=>FALSE,'error' => $this->upload->display_errors());
				var_dump($error);
			}else{
				$upload_data = $this->upload->data();
				$thumbName = $upload_data['file_name'];
				$img_width = $upload_data['image_width'];
				$img_height = $upload_data['image_height'];
				$this->load->library('image_lib');
					
				$config['image_library'] = 'gd2';
				$config['source_image'] = 'uploads/avatar/thumbs/'.$thumbName;
				$config['new_image'] = 'uploads/avatar/thumbs/'.$thumbName;
				$config['create_thumb'] = FALSE;
				$config['maintain_ratio'] = FALSE;
				if($img_width > $img_height){

					$config['width'] = '400';
					$config['height'] = round($img_height *(200/$img_width));
				}elseif($img_width == $img_height){
					$config['height'] = $config['width'] = '400';
				}else{
					$config['height'] = '400';
					$config['width'] = round($img_width *(200/$img_height));
				}
				$this->image_lib->initialize($config);
				$this->image_lib->resize();

				$user = aauth_user::find_by_id($_POST['id']);
				if($user->avatar){
					$file_to_delete = $user->avatar;
					$path_file = 'uploads/avatar/thumbs/'.$file_to_delete;
					unlink($path_file);
				}
				$user->avatar = $thumbName;
				$user->save();
				redirect('profile/'.$_POST['id']);
			}
		}else{
			redirect('profile');
		}
	}
	
	function defaultThumb()
	{
		if(!$_POST){
			redirect('error_404');
		}else{
			$id = $_POST['id'];
			$user = aauth_user::find_by_id($id);
			if($user->avatar){
					$file_to_delete = $user->avatar;
					$path_file = 'uploads/avatar/thumbs/'.$file_to_delete;
					unlink($path_file);
				}
				$user->avatar = null;
				$user->save();
				redirect('profile/'.$_POST['id']);
		}
	}
	
	
	
	function reset(){
		$user_id = $this->uri->segment(2);
		$code = $this->uri->segment(3);
		if(!$user_id || !$code){
			show_error('Veuillez re-vérifier votre informations. <a href="'.site_url('login').'">Page Connexion</a>');
			
		}else{
			$newPass = $this->aauth->reset_password($user_id,$code);
			if($newPass){
				echo $newPass;
				//$this->load->view($this->theme.'/auth/resetSuccess');
			}else{
				$this->load->view($this->theme.'/auth/resetFail');
			}
		}
	}
	
	
	
	
	private function checkValidEamil($email)
	{
		return filter_var($email, FILTER_VALIDATE_EMAIL);
				
	}





}